﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System.Collections.Concurrent;
using Microsoft.Office.Interop.Excel;
using SUNRPA.CONTROLS.IMPL.util;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class AddSheetNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            AddSheetNParam? param = item.ControlParam as AddSheetNParam;
            string excelObject = param.ExcelObject;
            Workbook wb = this.GetObjVar<ExcelModel>(ref excelObject, globleData).WorkBookValue as Workbook;
            if(wb == null) { 
                throw new RpaRuntimeException(String.Format("Excel对象{0}不存在", excelObject));
            }

            ExcelUtil excelUtil = new ExcelUtil();
            excelUtil.SetApp(wb);

            string sheetName = param.SheetName;
            this.GetStringVar(ref sheetName, globleData);

            int pos = param.AddPos;

            excelUtil.AddSheet(pos, sheetName);
            return currentIndex;
        }
    }
}
